/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-07-29     RT-Thread    first version
 */

/*------------------------------------------------------------------------------
 Section: Includes
 ------------------------------------------------------------------------------*/
#include <rtthread.h>
#include <rtdbg.h>

/*------------------------------------------------------------------------------
 Section: Macro Definitions
 ------------------------------------------------------------------------------*/
#define DBG_TAG                 "main"
#define DBG_LVL                 DBG_LOG
#define VERSION                 "v1.0"

#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
/*------------------------------------------------------------------------------
 Section: Type Definitions
 ------------------------------------------------------------------------------*/
typedef void (*entry_func_t)(void *arg);

typedef struct user_task_init_s
{
    const char      *name;           //名称
    entry_func_t    entry_func;    //入口函数
    void            *parameter;      //参数
    rt_uint32_t     stack_size;     //堆栈大小
    rt_uint8_t      priority;       //priority the priority of thread
    rt_uint32_t     tick;           //tick the time slice if there are same priority thread
}user_task_init_t;

/*------------------------------------------------------------------------------
 Section: Global Function prototypes
 ------------------------------------------------------------------------------*/
extern void Led_Thread(void *arg);
extern void Key_Thread(void *arg);
extern void Beep_Thread(void *arg);


/*------------------------------------------------------------------------------
 Section: Local Function prototypes
 ------------------------------------------------------------------------------*/
/* None */

/*------------------------------------------------------------------------------
 Section: Global Variables
 ------------------------------------------------------------------------------*/
/* None */

/*------------------------------------------------------------------------------
 Section: Local Variables
 ------------------------------------------------------------------------------*/
static const user_task_init_t task_table[] =
{
    {"led", Led_Thread, RT_NULL, 1024, 11, 10},
    {"key", Key_Thread, RT_NULL, 1024, 12, 10},
    {"beep", Beep_Thread, RT_NULL, 1024, 13, 10},
};



/*------------------------------------------------------------------------------
 Section: Function Definitions
 ------------------------------------------------------------------------------*/
/**
 ******************************************************************************
 * @brief   _Go
 * @param
 * @return
 * @details
 ******************************************************************************
 */
static void _Go(void)
{
    rt_kprintf("<#####################>\n");
    rt_kprintf("This is STM32H743 all_in_one.\n");
    rt_kprintf("Version: %s.\n", VERSION);
    rt_kprintf("<#####################>\n");
}

/**
 ******************************************************************************
 * @brief   _Thread_Init
 * @param
 * @return
 * @details
 ******************************************************************************
 */
static void _Thread_Init(void)
{
    const user_task_init_t* ptr;
    rt_thread_t tid;

    for(int i = 0; i < ARRAY_SIZE(task_table); i++)
    {
        ptr = &task_table[i];
        if(ptr == RT_NULL)
        {
            continue;
        }

        //创建线程
        tid = rt_thread_create(ptr->name, ptr->entry_func, ptr->parameter, ptr->stack_size, ptr->priority, ptr->tick);
        if(tid != RT_NULL)
        {
            rt_thread_startup(tid);
            rt_kprintf("user_task[%s] start.\n", ptr->name);
        }
        else
        {
            rt_kprintf("user_task[%s] error.\n", ptr->name);
        }

    }
}

/**
 ******************************************************************************
 * @brief   _Thread_Start
 * @param
 * @return
 * @details
 ******************************************************************************
 */
static void _Thread_Start(void)
{

}

/**
 ******************************************************************************
 * @brief   main
 * @param
 * @return
 * @details
 ******************************************************************************
 */
int main(void)
{
    int count = 1;

    _Go();
    _Thread_Init();
    _Thread_Start();


    while (count++)
    {
        LOG_D("Hello RT-Thread!");
        rt_kprintf("This is father thread.\n");
        rt_thread_mdelay(1000);
    }

    return RT_EOK;
}

/*------------------------------End of dddd.c----------------------------*/
